package com.mb.app.service.impl;


import com.baomidou.mybatisplus.service.impl.ServiceImpl;
import com.mb.app.dao.TbReportChannelMapper;
import com.mb.app.domain.TbReportChannel;
import com.mb.app.service.ITbReportChannelService;
import com.mb.common.utils.ExcelUtil;
import org.apache.poi.hssf.usermodel.HSSFWorkbook;
import org.springframework.stereotype.Service;

import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;
import java.io.*;
import java.math.BigDecimal;
import java.util.List;
import java.util.Map;

/**
 * <p>
 * 渠道PV/UV统计  服务实现类
 * </p>
 *
 * @author liliping
 * @since 2019-03-26
 */
@Service
public class TbReportChannelServiceImpl extends ServiceImpl<TbReportChannelMapper, TbReportChannel> implements ITbReportChannelService {


    @Override
    public List<TbReportChannel> queryList(Map<String, Object> map) {
        List<TbReportChannel> list = this.baseMapper.lst(map);
        return list;
    }

    @Override
    public int count(Map<String, Object> map) {
        return this.baseMapper.countt(map);
    }

    @Override
    public void exprot( HttpServletResponse response,Map<String, Object> map)  throws Exception {

        //获取数据
        List<TbReportChannel> list = this.baseMapper.lst(map);
        //excel标题
        String[] title = {"渠道名称", "App名称", "统计时间", "PV", "UV","注册用户","UV转化率","存量用户","反馈用户","反馈UV转化率","登录成功","登录转化率","点击人数","点击人数占比","点击次数","老用户点击人数","老用户点击次数","人均点击次数"};

        String reportDate = map.get("reportDate").toString();
        //excel文件名
        String fileName = "渠道PV-UV统计" + reportDate + ".xls";

        //sheet名
        String sheetName = "渠道PV-UV统计";
        String [][] content  = new String[list.size()][title.length];
        for (int i = 0; i < list.size(); i++) {
            TbReportChannel obj = list.get(i);
            content[i][0] = obj.getChannelName();
            content[i][1] = obj.getAppName();
            content[i][2] = obj.getReportDate().toString();
            content[i][3] = obj.getPv().toString();
            content[i][4] = obj.getUv().toString();
            content[i][5] = obj.getTodayRegCount().toString();
            if(obj.getUvConvertRate() != null) {
                BigDecimal bigDecimal = new BigDecimal(100);
                BigDecimal bd=obj.getUvConvertRate().setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal rate = bd.multiply(bigDecimal);
                rate = rate.setScale(0,BigDecimal.ROUND_HALF_UP);
                content[i][5] = rate.toString()+"%";
            }else {
                content[i][5] = "";
            }
            content[i][7] = obj.getRegCount().toString();
            content[i][8] = obj.getFeedbackCount().toString();
            if(obj.getUvFeedbackConvertRate() != null) {
                BigDecimal bigDecimal = new BigDecimal(100);
                BigDecimal bd=obj.getUvFeedbackConvertRate().setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal rate = bd.multiply(bigDecimal);
                rate = rate.setScale(0,BigDecimal.ROUND_HALF_UP);
                content[i][9] = rate.toString()+"%";
            }else {
                content[i][9] = "";
            }
            content[i][10] = obj.getLoginCount().toString();
            if(obj.getLoginConvertRate() != null) {
                BigDecimal bigDecimal = new BigDecimal(100);
                BigDecimal bd=obj.getLoginConvertRate().setScale(2, BigDecimal.ROUND_HALF_UP);
                BigDecimal rate = bd.multiply(bigDecimal);
                rate = rate.setScale(0,BigDecimal.ROUND_HALF_UP);
                content[i][11] = rate.toString()+"%";
            }else {
                content[i][11] = "";
            }
            content[i][12] = obj.getUserClickCount().toString();
            content[i][13] = obj.getClickRate().toString();
            content[i][14] = obj.getClickTimes().toString();
            content[i][15] = obj.getOldUserClickCount().toString();
            content[i][16] = obj.getOldUserClickTimes().toString();
            content[i][17] = obj.getAvgClickTimes().toString();

        }

        //创建HSSFWorkbook
        HSSFWorkbook wb = ExcelUtil.getHSSFWorkbook(sheetName, title, content, null);

        //响应到客户端
        try {
            ByteArrayOutputStream os = new ByteArrayOutputStream();
            wb.write(os);
            byte[] contents = os.toByteArray();
            InputStream is = new ByteArrayInputStream(contents);
            response.reset();
            this.setResponseHeader(response, fileName);
            ServletOutputStream out = response.getOutputStream();
            BufferedInputStream bis = null;
            BufferedOutputStream bos = null;

            try {
                bis = new BufferedInputStream(is);
                bos = new BufferedOutputStream(out);
                byte[] buff = new byte[2048];
                int bytesRead;
                while (-1 != (bytesRead = bis.read(buff, 0, buff.length))) {
                    bos.write(buff, 0, bytesRead);
                }
            } catch (Exception e) {
                e.printStackTrace();
            } finally {
                if (bis != null)
                    bis.close();
                if (bos != null)
                    bos.close();
            }
        } catch (Exception e) {
            e.printStackTrace();
        }

    }
    //发送响应流方法
    public void setResponseHeader(HttpServletResponse response, String fileName) {
        try {
            try {
                fileName = new String(fileName.getBytes(),"ISO8859-1");
            } catch (UnsupportedEncodingException e) {
                // TODO Auto-generated catch block
                e.printStackTrace();
            }
//            response.setContentType("application/octet-stream;charset=ISO8859-1");
            response.setContentType("application/vnd.ms-excel");
            response.setHeader("Content-Disposition", "attachment;filename="+ fileName);
            response.addHeader("Pargam", "no-cache");
            response.addHeader("Cache-Control", "no-cache");
        } catch (Exception ex) {
            ex.printStackTrace();
        }
    }
}
